package com.ficus.road.maintain.controller;

import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.service.EmergencyService;
import com.ficus.road.maintain.service.HitService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * @author xiawen yang
 * @date 2021/8/13 上午11:18
 */
@RestController
@RequestMapping("/emergency")
public class EmergencyController {

    private final static String Tree_Fall_Down = "PIC_TREE_DISEASE_RESULT_DISEASE_FALL_DOWN";

    private final static String Fence_Fall_Down = "PIC_FENCE_DISEASE_RESULT_DISEASE_FALL_DOWN";

    @Autowired
    private EmergencyService emergencyService;

    @Autowired
    private HitService hitService;

    @PostMapping("/xlj/query")
    @PreAuthorize("@rm.check('EMERGENCY')")
    public ResponseEntity<List<XljVO>> getXljListByQuery(@Min(0) @Valid @RequestParam(defaultValue = "0") Integer start,
                                                         @Min(1) @Valid @RequestParam(defaultValue = "10000") Integer limit,
                                                         @RequestBody XljQueryRequest xljQueryRequest) {
        PageInfo<XljVO> xljList = emergencyService.queryXljList(start, limit, xljQueryRequest);

        long total = xljList.getTotal();
        Boolean isFinal = Boolean.FALSE;
        if (xljList.getList().size() < limit) {
            isFinal = Boolean.TRUE;
        }
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("X-OneAPI-Exhausted", String.valueOf(isFinal));
        headers.add("X-OneAPI-EstimatedSize", String.valueOf(total));

        return new ResponseEntity<>(xljList.getList(), headers, HttpStatus.OK);
    }

    @GetMapping("/xlj/query")
    @PreAuthorize("@rm.check('EMERGENCY')")
    public ResponseEntity<List<XljVO>> getXljList(@Min(0) @Valid @RequestParam(defaultValue = "0") Integer start,
                                                  @Min(1) @Valid @RequestParam(defaultValue = "10000") Integer limit) {
        PageInfo<XljVO> xljList = emergencyService.queryXljList(start, limit, null);

        long total = xljList.getTotal();
        Boolean isFinal = Boolean.FALSE;
        if (xljList.getList().size() < limit) {
            isFinal = Boolean.TRUE;
        }
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("X-OneAPI-Exhausted", String.valueOf(isFinal));
        headers.add("X-OneAPI-EstimatedSize", String.valueOf(total));

        return new ResponseEntity<>(xljList.getList(), headers, HttpStatus.OK);
    }

    @PostMapping("/xlj/statistic")
    @PreAuthorize("@rm.check('EMERGENCY')")
    public ResponseEntity<XljStatisticVO> getXljStatistic(@RequestBody XljQueryRequest xljQueryRequest) {
        XljStatisticVO xljStatisticVO = emergencyService.queryXljStatistic(xljQueryRequest);
        return new ResponseEntity<>(xljStatisticVO, HttpStatus.OK);
    }

    /**
     * 获取下立交历史数据
     * @param year
     * @param uniIdHuwan
     * @return
     */
    @GetMapping("/xlj/{year}/{uniIdHuwan}")
//    @PreAuthorize("@rm.check('EMERGENCY')")
    public ResponseEntity<XljHistoryVO> getXljHistory(@PathVariable("year") String year, @PathVariable("uniIdHuwan") String uniIdHuwan) {
        XljHistoryVO xljHistoryVOS = emergencyService.queryXljHistory(year, uniIdHuwan);
        return new ResponseEntity<>(xljHistoryVOS, HttpStatus.OK);
    }

    @GetMapping("/fence/query")
    @PreAuthorize("@rm.check('EMERGENCY')")
    public ResponseEntity<List<FallDownVO>> getFenceFallInfo(@Min(0) @Valid @RequestParam(defaultValue = "0") Integer start,
                                                             @Min(1) @Valid @RequestParam(defaultValue = "10000") Integer limit) {
        PageInfo<FallDownVO> fallDownVOS = emergencyService.queryFallDown(start, limit, Fence_Fall_Down);
        long total = fallDownVOS.getTotal();
        Boolean isFinal = Boolean.FALSE;
        if (fallDownVOS.getList().size() < limit) {
            isFinal = Boolean.TRUE;
        }
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("X-OneAPI-Exhausted", String.valueOf(isFinal));
        headers.add("X-OneAPI-EstimatedSize", String.valueOf(total));

        return new ResponseEntity<>(fallDownVOS.getList(), HttpStatus.OK);
    }

    @GetMapping("/tree/query")
    @PreAuthorize("@rm.check('EMERGENCY')")
    public ResponseEntity<List<FallDownVO>> getTreeFallInfo(@Min(0) @Valid @RequestParam(defaultValue = "0") Integer start,
                                                            @Min(1) @Valid @RequestParam(defaultValue = "10000") Integer limit) {
        PageInfo<FallDownVO> fallDownVOS = emergencyService.queryFallDown(start, limit, Tree_Fall_Down);
        long total = fallDownVOS.getTotal();
        Boolean isFinal = Boolean.FALSE;
        if (fallDownVOS.getList().size() < limit) {
            isFinal = Boolean.TRUE;
        }
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("X-OneAPI-Exhausted", String.valueOf(isFinal));
        headers.add("X-OneAPI-EstimatedSize", String.valueOf(total));

        return new ResponseEntity<>(fallDownVOS.getList(), headers, HttpStatus.OK);
    }
}
